<?php

    require_once("inc.php");
    require_once("hla_lib.inc.php");

    function OutError($msg)
    {
        global $HLA_ThemeCtrl;

        $HLA_ThemeCtrl->Display('page_header');
        $HLA_ThemeCtrl->Msg($msg, false);
        $HLA_ThemeCtrl->Display('page_footer');

        exit;
    }

    if (empty($_GET['sample_id']))
        OutError(MI_CANNOT_PRINT_SAMPLE_NOT_EXISTS);

    $sample_id = strtoupper($_GET['sample_id']);

    $sample = $HLA_DBTool->ExecAssocRows(
                "select * from hla_sample \n".
                "where sample_id = ? \n",
                array($sample_id));

    if (count($sample) <= 0)
        OutError(MI_CANNOT_PRINT_SAMPLE_NOT_EXISTS);

    $sample = $sample[0];

    # comment this out so we can print even if sample is not finalized
    # this need to be fixed pending what happens to finalize - capability
    #if ($sample['final'] != 'Y')
    #    OutError(MI_CANNOT_PRINT_SAMPLE_NOT_FINAL);

    $kit = $HLA_DBTool->ExecAssocRows(
                "select * from hla_kit \n".
                "where kit_id = ? \n",
                array($sample['kit_id']));

    if (count($kit) <= 0)
        OutError(MI_ERROR_KITS);

    $kit = $kit[0];

    $sample_types = null;

    $sample_types = $HLA_DBTool->ExecAssocRows(
                "select serology_id, line_no1, line_no2, selected from hla_sample_types \n".
                "where sample_id = ? \n".
                "and imperfect = 'N' \n".
                "and frequency > 0 \n".
                "order by serology_id",
                array($sample_id));

    if (count($sample_types) <= 0)
        OutError(MI_INVALID_SAMPLE);

    $typed_alleles = array();
    $middle_alleles = array();

    foreach ($sample_types as $row)
    {
        $line_no_list = explode(",", $row['line_no1']);

        $serotypes = array();
        $genotype_summary = array();
        $genotypes = array();
        $middle_alleles_part = '';

        foreach ($line_no_list as $line_no)
        {
            $line_no = ltrim($line_no, '0');
            $pattern = $HLA_DBTool->ExecAssocRows(
                        "select serotype, genotype from hla_pattern \n".
                        "where kit_id = ? \n".
                        "and serology_id = ? \n".
                        "and line_no = ".intval($line_no)." \n",
                        array($sample['kit_id'], $row['serology_id']));

            $serotypes[] = $pattern[0]['serotype'];
            $genotypes[] = $pattern[0]['genotype'];
            $genotype_summary[] = preg_replace("/(\w+\*\d{2})(.*)/", "$1", $pattern[0]['genotype']);
        }

        if ($row['selected'] == 'Y')
        {
            $serotypes = array_unique($serotypes);

            $typed_alleles[$row['serology_id']]['serotypes'] = implode("/", $serotypes);
	    $typed_alleles[$row['serology_id']]['genotype_summary'] = implode("/", $genotype_summary);
        }

        $middle_alleles_part['genotypes'] = implode("/", $genotypes);

        $serotypes = array();
        $genotype_summary = array();
        $genotypes = array();

        $line_no_list = explode(",", $row['line_no2']);
        foreach ($line_no_list as $line_no)
        {
            $line_no = ltrim($line_no, '0');
            $pattern = $HLA_DBTool->ExecAssocRows(
                        "select serotype, genotype from hla_pattern \n".
                        "where kit_id = ? \n".
                        "and serology_id = ? \n".
                        "and line_no = ".intval($line_no)." \n",
                        array($sample['kit_id'], $row['serology_id']));

            $serotypes[] = $pattern[0]['serotype'];
            $genotypes[] = $pattern[0]['genotype'];
            $genotype_summary[] = preg_replace("/(\w+\*\d{2})(.*)/", "$1", $pattern[0]['genotype']);
        }

        if ($row['selected'] == 'Y')
        {
            $serotypes = array_unique($serotypes);

            $typed_alleles[$row['serology_id']]['serotypes'] .= '||'.implode("/", $serotypes);
            $typed_alleles[$row['serology_id']]['genotype_summary'] .= '||'.implode("/", $genotype_summary);
        }

        $middle_alleles_part['genotypes'] .= '||'.implode("/", $genotypes);
        $middle_alleles_part['serology_id'] = $row['serology_id'];

        if ($row['selected'] == 'Y')
            $middle_alleles[0][] = $middle_alleles_part;
        else
            $middle_alleles[1][] = $middle_alleles_part;
    }

    $patient = null;
    if ($sample['patient_rowguid'] >= 0)
    {
        $patient = $HLA_DBTool->ExecAssocRows(
                    "select * from hla_patient \n".
                    "where rowguid = ".$sample['patient_rowguid']."\n");
        if (count($patient) <= 0)
            $patient = null;
        else
            $patient = $patient[0];
    }

    function DrawImagePDF($left, $top, $width, $height, $image_file_name)
    {
        global $cpdf;

        $image_rotate = 0;
        if (isset($_COOKIE['img_rotate']))
            $image_rotate = $_COOKIE['img_rotate'];
        switch ($image_rotate)
        {
            case 0:
                cpdf_import_jpeg($cpdf, ROOT_REAL_PATH."/sample_images/".$image_file_name,
                        $left-$width,
                        $top-$height-5,
                        0,
                        $width, $height,
                        100, 100, 1);
                break;
            case 1:
                cpdf_import_jpeg($cpdf, ROOT_REAL_PATH."/sample_images/".$image_file_name,
                        $left-$width,
                        $top-5,
                        270,
                        $height, $width,
                        100, 100, 1);
                break;
            case 2:
                cpdf_import_jpeg($cpdf, ROOT_REAL_PATH."/sample_images/".$image_file_name,
                        $left-$width,
                        $top-$height-5,
                        0,
                        $width, $height,
                        100, 100, 1);
                break;
            case 3:
                cpdf_import_jpeg($cpdf, ROOT_REAL_PATH."/sample_images/".$image_file_name,
                        $left,
                        $top-$height-5,
                        90,
                        $height, $width,
                        100, 100, 1);
                break;
        }
    }

    function DrawPageHeader()
    {
        global $cpdf, $report_width, $report_height, $report_margin,
            $report_logo_right, $report_logo_bottom, $report_logo_width, $report_logo_height,
            $report_title_left, $report_title_bottom, $report_title_width, $report_title_height,
            $sample_image_width, $sample_image_height,
            $sample_info_text_left1, $sample_info_text_left2, $sample_info_text_left3,
            $alleles_text_left1, $alleles_text_left2, $alleles_text_left3,
            $line_space, $text_space, $ansi_encoding,
            $sample_info_font_name, $alleles_title_font_name, $alleles_font_name, $footer_font_name,
            $custom_encoding, $custom_font_name, $params, $typed_alleles, $line_top, $top_box_bottom,
            $sample, $patient, $kit;

    ////////// Page Header Images //////////

        cpdf_import_jpeg($cpdf, ROOT_REAL_PATH."/report_logo.jpg",
                $report_width-$report_logo_width-$report_logo_right,
                $report_height-$report_logo_height-$report_logo_bottom,
                0,
                $report_logo_width, $report_logo_height,
                100, 100, 1);

        cpdf_import_jpeg($cpdf, ROOT_REAL_PATH."/report_title.jpg",
                $report_title_left,
                $report_height-$report_logo_height-$report_title_bottom,
                0,
                $report_title_width, $report_title_height,
                100, 100, 1);

        $line_top = $report_height-$report_logo_height-$report_logo_bottom;
        $right_text_xpos = $report_width-200;

        cpdf_moveto($cpdf, $report_margin, $line_top);
        cpdf_lineto($cpdf, $report_width-$report_margin, $line_top);
        cpdf_stroke($cpdf);

    ////////// Title //////////

        cpdf_begin_text($cpdf);
        cpdf_set_text_rendering($cpdf, 0);

        cpdf_set_font($cpdf, $custom_font_name, 16, $custom_encoding);

        if (empty($params['institution_name']))
            $params['institution_name'] = MI_PARAM_N_INSTITUTION_NAME;

        if (empty($params['lib_name']))
            $params['lib_name'] = MI_PARAM_N_LIB_NAME;

        if (empty($params['report_name']))
            $params['report_name'] = MI_PARAM_N_REPORT_NAME;

        $line_top -= 20;
        cpdf_text($cpdf, mb_convert_encoding($params['institution_name'], OS_CHARSET), $report_margin+10, $line_top);

        cpdf_set_font($cpdf, $custom_font_name, 14, $custom_encoding);

        $line_top -= 18;
        cpdf_text($cpdf, mb_convert_encoding($params['lib_name'], OS_CHARSET), $report_margin+10, $line_top);

        $line_top -= 18;
        cpdf_text($cpdf, mb_convert_encoding($params['report_name'], OS_CHARSET), $report_margin+10, $line_top);

        cpdf_end_text($cpdf);

        $line_top -= $line_space;

    ////////// Box Top Line //////////

        $top_box_top = $line_top;
        $top_box_right = $report_width-$report_margin;
        $top_box_left = $report_margin;
        $top_box_middle = ($report_width-$report_margin) / 2;

        cpdf_moveto($cpdf, $top_box_left, $top_box_top);
        cpdf_lineto($cpdf, $top_box_right, $top_box_top);
        cpdf_stroke($cpdf);

    ////////// Box First Text Block //////////

        cpdf_begin_text($cpdf);
        cpdf_set_text_rendering($cpdf, 0);

        $right_text_xpos = $report_width-280;

        $line_top -= $text_space;
        cpdf_set_font($cpdf, $sample_info_font_name, 12, $ansi_encoding);
        cpdf_text($cpdf, 'Sample ID', $sample_info_text_left1, $line_top);
        cpdf_text($cpdf, ': '.$sample['sample_id'], $sample_info_text_left2, $line_top);

        $line_top -= $text_space;
        cpdf_set_font($cpdf, $sample_info_font_name, 12, $ansi_encoding);
        cpdf_text($cpdf, 'Patient ID', $sample_info_text_left1, $line_top);
        cpdf_text($cpdf, ':', $sample_info_text_left2, $line_top);

        cpdf_set_font($cpdf, $custom_font_name, 12, $custom_encoding);
        if ($patient!=null)
            cpdf_text($cpdf, mb_convert_encoding($patient['patient_id'], OS_CHARSET), $sample_info_text_left3, $line_top);

        $line_top -= $text_space;
        cpdf_set_font($cpdf, $sample_info_font_name, 12, $ansi_encoding);
        cpdf_text($cpdf, 'Name', $sample_info_text_left1, $line_top);
        cpdf_text($cpdf, ': ', $sample_info_text_left2, $line_top);

        cpdf_set_font($cpdf, $custom_font_name, 12, $custom_encoding);
        if ($patient!=null)
            cpdf_text($cpdf, mb_convert_encoding($patient['first_name'], OS_CHARSET).' '.
                    mb_convert_encoding($patient['last_name'], OS_CHARSET), $sample_info_text_left3, $line_top);

        $line_top -= $text_space;
        cpdf_set_font($cpdf, $sample_info_font_name, 12, $ansi_encoding);
        cpdf_text($cpdf, 'Gender', $sample_info_text_left1, $line_top);
        cpdf_text($cpdf, ':', $sample_info_text_left2, $line_top);

        cpdf_set_font($cpdf, $custom_font_name, 12, $custom_encoding);
        if ($patient!=null && ($patient['sex'] == 'M' || $patient['sex'] == 'F'))
            cpdf_text($cpdf, mb_convert_encoding($patient['sex']=='M'?_MALE:_FEMALE, OS_CHARSET), $sample_info_text_left3, $line_top);

        $line_top -= $text_space;
        cpdf_set_font($cpdf, $sample_info_font_name, 12, $ansi_encoding);
        cpdf_text($cpdf, 'Kit name', $sample_info_text_left1, $line_top);
        cpdf_text($cpdf, ': '.$kit['kit_desc'], $sample_info_text_left2, $line_top);

        $line_top -= $text_space;
        cpdf_set_font($cpdf, $sample_info_font_name, 12, $ansi_encoding);
        cpdf_text($cpdf, 'Kit Lot#', $report_margin+10, $line_top);
        cpdf_text($cpdf, ': '.$sample['lot_no'], $sample_info_text_left2, $line_top);

        $line_top -= $text_space;
        cpdf_set_font($cpdf, $sample_info_font_name, 12, $ansi_encoding);
        cpdf_text($cpdf, 'Kit S/N', $sample_info_text_left1, $line_top);
        cpdf_text($cpdf, ': '.$kit['kit_id'], $sample_info_text_left2, $line_top);

        $line_top -= $text_space;
        cpdf_set_font($cpdf, $sample_info_font_name, 12, $ansi_encoding);
        cpdf_text($cpdf, 'Code', $sample_info_text_left1, $line_top);
        cpdf_text($cpdf, ': '.$kit['kit_sn'], $sample_info_text_left2, $line_top);

        $line_top -= $text_space;
        cpdf_set_font($cpdf, $sample_info_font_name, 12, $ansi_encoding);
        cpdf_text($cpdf, 'Printed Date', $sample_info_text_left1, $line_top);
        cpdf_text($cpdf, ': '.date('Y/m/d'), $sample_info_text_left2, $line_top);

        cpdf_end_text($cpdf);

        $line_top -= $line_space;

    ////////// Box Middle Horizontal Line //////////

        cpdf_moveto($cpdf, $top_box_left, $line_top);
        cpdf_lineto($cpdf, $top_box_middle, $line_top);
        cpdf_stroke($cpdf);

    ////////// Box Alleles Summary //////////

        cpdf_begin_text($cpdf);
        cpdf_set_text_rendering($cpdf, 0);

        $line_top -= $text_space;

        cpdf_set_font($cpdf, $alleles_title_font_name, 12, $ansi_encoding);

	cpdf_text($cpdf, 'Summary: (Genotype [SeroType])', $alleles_text_left1, $line_top);

        cpdf_set_font($cpdf, $alleles_font_name, 12, $ansi_encoding);

        $line_top -= $text_space;

        foreach ($typed_alleles as $serology_id => $serology_type)
        {
            cpdf_text($cpdf, $serology_id, $alleles_text_left2, $line_top);

            $output_text = $serology_type['genotype_summary'].' ['.$serology_type['serotypes'].']';
            $line_text_length = 27;

            while ($output_text != '')
            {
                $out_text = substr($output_text, 0, $line_text_length);
                $output_text = substr($output_text, $line_text_length);

                cpdf_text($cpdf, $out_text, $alleles_text_left3, $line_top);

                $line_top -= $text_space;
            }
        }

        $line_top += $text_space;

        cpdf_end_text($cpdf);

    ////////// Sample Images //////////

        if (!empty($sample['image_file_name']))
        {
            $image_file_names = explode(",", $sample['image_file_name']);
            if (count($image_file_names) == 1)
            {
                if (!empty($image_file_names[0]) && is_file(ROOT_REAL_PATH."/sample_images/".$image_file_names[0]))
                    DrawImagePDF($top_box_middle+$sample_image_width*1.5+$line_space, $top_box_top-$line_space, $sample_image_width, $sample_image_height, $image_file_names[0]);
            }
            else if (count($image_file_names) > 1)
            {
                if (!empty($image_file_names[0]) && is_file(ROOT_REAL_PATH."/sample_images/".$image_file_names[0]))
                    DrawImagePDF($top_box_middle+$sample_image_width+$line_space, $top_box_top-$line_space, $sample_image_width, $sample_image_height, $image_file_names[0]);
                if (!empty($image_file_names[1]) && is_file(ROOT_REAL_PATH."/sample_images/".$image_file_names[1]))
                    DrawImagePDF($top_box_middle+($sample_image_width+$line_space)*2, $top_box_top-$line_space, $sample_image_width, $sample_image_height, $image_file_names[1]);
            }
        }
        else
        {
            cpdf_begin_text($cpdf);
            cpdf_set_text_rendering($cpdf, 0);

            $line_top -= $text_space;

            cpdf_set_font($cpdf, $footer_font_name, 20, $ansi_encoding);

            cpdf_text($cpdf, "Photo", $top_box_middle+115, $top_box_top-120);

            $line_top -= $text_space;

            cpdf_text($cpdf, "Documentation", $top_box_middle+75, $top_box_top-140);

            cpdf_end_text($cpdf);
        }

    ////////// Box Middle Horizontal Line //////////

        $line_top -= $line_space;
        $top_box_bottom = $line_top;

        if ($top_box_bottom > $top_box_top-$line_space*4-$sample_image_height)
            $top_box_bottom = $top_box_top-$line_space*4-$sample_image_height;

        cpdf_moveto($cpdf, $top_box_left, $top_box_bottom);
        cpdf_lineto($cpdf, $top_box_right, $top_box_bottom);
        cpdf_stroke($cpdf);

    ////////// Box Left Vertical Line by ian//////////

        cpdf_moveto($cpdf, $top_box_left, $top_box_top);
        cpdf_lineto($cpdf, $top_box_left, $top_box_bottom);
        cpdf_stroke($cpdf);
        
       $primer_box_top = $top_box_bottom;
       $primer_box_bottom = $top_box_bottom - 18;
        
       cpdf_moveto($cpdf, $top_box_left, $primer_box_top);
       cpdf_lineto($cpdf, $top_box_left, $primer_box_bottom);
       cpdf_stroke($cpdf);
       
       cpdf_moveto($cpdf, $top_box_right, $primer_box_top);
       cpdf_lineto($cpdf, $top_box_right, $primer_box_bottom);
       cpdf_stroke($cpdf);
       
       cpdf_moveto($cpdf, $top_box_left, $primer_box_bottom);
       cpdf_lineto($cpdf, $top_box_right, $primer_box_bottom);
       cpdf_stroke($cpdf);

    ////////// Box Middle Vertical Line //////////

        cpdf_moveto($cpdf, $top_box_middle, $top_box_top);
        cpdf_lineto($cpdf, $top_box_middle, $top_box_bottom);
        cpdf_stroke($cpdf);

    ////////// Box Right Line //////////

        cpdf_moveto($cpdf, $top_box_right, $top_box_top);
        cpdf_lineto($cpdf, $top_box_right, $top_box_bottom);
        cpdf_stroke($cpdf);
    }

    function DrawPageFooter()
    {
        global $cpdf, $report_width, $report_height, $report_margin,
            $report_logo_right, $report_logo_bottom, $report_logo_width, $report_logo_height,
            $report_title_left, $report_title_bottom, $report_title_width, $report_title_height,
            $sample_image_width, $sample_image_height,
            $sample_info_text_left1, $sample_info_text_left2, $sample_info_text_left3,
            $alleles_text_left1, $alleles_text_left2, $alleles_text_left3,
            $line_space, $text_space, $ansi_encoding,
            $sample_info_font_name, $alleles_title_font_name, $alleles_font_name, $footer_font_name,
            $custom_encoding, $custom_font_name,
            $params, $typed_alleles, $line_top, $page_footer_top,
            $page_no, $page_total, $sample;

        $line_top = $page_footer_top;

        cpdf_moveto($cpdf, $report_margin, $line_top);
        cpdf_lineto($cpdf, $report_width-$report_margin, $line_top);
        cpdf_stroke($cpdf);

        $line_top -= $text_space;

        cpdf_begin_text($cpdf);
        cpdf_set_text_rendering($cpdf, 0);

        cpdf_set_font($cpdf, $footer_font_name, 12, $ansi_encoding);

        cpdf_text($cpdf, 'Copyright (c) 1999-2007 Texas BioGene, Inc.', $report_margin, $line_top);
        cpdf_text($cpdf, 'Estabilshed by Account:', $report_margin+260, $line_top);

        cpdf_set_font($cpdf, $custom_font_name, 12, $custom_encoding);

        $test_by = mb_convert_encoding($sample['test_by'], OS_CHARSET);
        cpdf_text($cpdf, $test_by, $report_margin+380, $line_top);

        $line_top -= 12;

        cpdf_set_font($cpdf, $footer_font_name, 12, $ansi_encoding);

        cpdf_text($cpdf, 'All rights reserved', $report_margin, $line_top);

        cpdf_text($cpdf, 'Page: '.$page_no.' of '.$page_total, $report_margin+360, $line_top);

        cpdf_end_text($cpdf);
    }

    $params = $HLA_DBTool->ExecKeyValues(
                "select param_key, param_value from hla_param \n".
                "order by param_key");

    $report_width = 595;
    $report_height = 842;
    $report_margin = 30;

    $report_logo_right = $report_margin;
    $report_logo_bottom = $report_margin;
    $report_logo_width = 150;
    $report_logo_height = 30;

    $report_title_left = $report_margin;
    $report_title_bottom = $report_margin-2;
    $report_title_width = 210;
    $report_title_height = 22;

    $page_footer_top = 60;

    $sample_image_width = 130;
    $sample_image_height = 182;

    $sample_info_text_left1 = $report_margin+10;
    $sample_info_text_left2 = $report_margin+75;
    $sample_info_text_left3 = $report_margin+81;

    $alleles_text_left1 = $report_margin+5;
    $alleles_text_left2 = $report_margin+10;
    $alleles_text_left3 = $report_margin+50;

    $line_space = 7;
    $text_space = 14;

    $page_no = 1;
    $page_total = 1;

    $ansi_encoding = "WinAnsiEncoding";
    $sample_info_font_name = "Times";
    $alleles_title_font_name = "Times";
    $alleles_font_name = "Courier-Bold";
    $footer_font_name = "Times-Italic";

    $custom_encoding = "WinAnsiEncoding";
    $custom_font_name = "Times";

    switch (OS_CHARSET)
    {
        case 'big5':
            $custom_encoding = "ETen-B5-H";
            $custom_font_name = "MHei-Medium";
            break;

        case 'gbk':
            $custom_encoding = "GBK-EUC-H";
            $custom_font_name = "STSong-Light";
            break;

        case 'sjis':
            $custom_encoding = "90msp-RKSJ-H";
            $custom_font_name = "HeiseiMin-W3";
            break;

        case 'koi8-r':
            $custom_encoding = "KSCms-UHC-H";
            $custom_font_name = "HYGoThic-Medium";
            break;
    }

////////// PDF Start //////////

    $cpdf = cpdf_open(0);

    $output_text = '';
    $middle_alleles_index = 0;
    $middle_alleles_sub_index = 0;
    $first_page = true;

    $result_inner_font_size = 9;

    while ($middle_alleles_index < 2)
    {
        cpdf_page_init($cpdf, $page_no, 0, $report_height, $report_width, 1.0);

        DrawPageHeader();

        $line_text_length = 88;
        $line_top = $top_box_bottom - $text_space -19;

        cpdf_begin_text($cpdf);
        cpdf_set_text_rendering($cpdf, 0);
        
//positive wells
            cpdf_set_font($cpdf, $alleles_title_font_name, 10, $ansi_encoding);
            cpdf_text($cpdf, "Positive Wells:", $alleles_text_left1, $line_top+20);
            cpdf_text($cpdf, $sample['primer_numbers'], $alleles_text_left1+64, $line_top+20);
//positive wells
     
        if ($first_page)
        {
            cpdf_set_font($cpdf, $alleles_title_font_name, 12, $ansi_encoding);
            cpdf_text($cpdf, "Typing Result:", $alleles_text_left1, $line_top+3);
           
            $line_top -= $text_space;
	    $first_page = false;
        }
        else if ($output_text != '')
        {
            cpdf_set_font($cpdf, $alleles_font_name, $result_inner_font_size, $ansi_encoding);

            while ($output_text != '')
            {
                $out_text = substr($output_text, 0, $line_text_length);
                $output_text = substr($output_text, $line_text_length);

                cpdf_text($cpdf, $out_text, $alleles_text_left3, $line_top);

                $line_top -= $text_space;
            }

            if ($output_text == '')
                $middle_alleles_sub_index++;

            if ($middle_alleles_sub_index >= count($middle_alleles[$middle_alleles_index]))
            {
                $middle_alleles_index++;
                $middle_alleles_sub_index = 0;

                if ($middle_alleles_index == 1)
                {
                    cpdf_set_font($cpdf, $alleles_title_font_name, 12, $ansi_encoding);
                    cpdf_text($cpdf, "Other Possible Result:", $alleles_text_left1, $line_top);
                    $line_top -= $text_space;
                }
            }
        }

        do
        {
            cpdf_set_font($cpdf, $alleles_font_name, $result_inner_font_size, $ansi_encoding);

	    cpdf_text($cpdf, $middle_alleles[$middle_alleles_index][$middle_alleles_sub_index]['serology_id'], $alleles_text_left2, $line_top);

            $output_text = $middle_alleles[$middle_alleles_index][$middle_alleles_sub_index]['genotypes'];

            while ($output_text != '')
            {
                if ($line_top - $text_space < $page_footer_top)
                    break;

                $out_text = substr($output_text, 0, $line_text_length);
                $output_text = substr($output_text, $line_text_length);

                cpdf_text($cpdf, $out_text, $alleles_text_left3, $line_top);

                $line_top -= $text_space;
            }

            if ($output_text == '')
                $middle_alleles_sub_index++;

            if ($middle_alleles_sub_index >= count($middle_alleles[$middle_alleles_index]))
            {
                $middle_alleles_index++;
                $middle_alleles_sub_index = 0;

                if ($middle_alleles_index == 1)
                {
                    cpdf_end_text($cpdf);

                    $line_top += 5;
                    cpdf_moveto($cpdf, $report_margin, $line_top);
                    cpdf_lineto($cpdf, $report_width-$report_margin, $line_top);
                    cpdf_stroke($cpdf);
                    $line_top -= $text_space;

                    cpdf_begin_text($cpdf);
                    cpdf_set_text_rendering($cpdf, 0);
                    cpdf_set_font($cpdf, $alleles_title_font_name, 12, $ansi_encoding);
                    cpdf_text($cpdf, "Other Possible Result:", $alleles_text_left1, $line_top);
                    $line_top -= $text_space;
                }
            }

            if ($line_top - $text_space < $page_footer_top)
                break;
        } while ($middle_alleles_index < 2);

        cpdf_end_text($cpdf);

        if ($middle_alleles_index >= 2)
        {
            /*cpdf_moveto($cpdf, $report_margin, $line_top);
            cpdf_lineto($cpdf, $report_margin+212, $line_top);
            cpdf_stroke($cpdf);

            cpdf_moveto($cpdf, $report_margin+293, $line_top);
            cpdf_lineto($cpdf, $report_width-$report_margin, $line_top);
            cpdf_stroke($cpdf);*/

            cpdf_begin_text($cpdf);
            cpdf_set_text_rendering($cpdf, 0);

            //cpdf_set_font($cpdf, $sample_info_font_name, 12, $ansi_encoding);

            //cpdf_text($cpdf, 'End Of Report', $report_margin+218, $line_top-3);

            $line_top -= $text_space;

            if ($line_top < 130)
            {
                cpdf_end_text($cpdf);

                $page_no++;

                cpdf_page_init($cpdf, $page_no, 0, $report_height, $report_width, 1.0);

                DrawPageHeader();

                cpdf_begin_text($cpdf);
                cpdf_set_text_rendering($cpdf, 0);
            }

            $line_top = 130;

            cpdf_set_font($cpdf, $footer_font_name, 13, $ansi_encoding);

            cpdf_text($cpdf, 'Comment:', $report_margin, $line_top);

            cpdf_set_font($cpdf, $custom_font_name, 13, $custom_encoding);

            $line_top -= 2;

            $comment = mb_convert_encoding($sample['comment'], OS_CHARSET);
            $text_left = $report_margin+60;

            while ($comment != '')
            {
                $out_text = mb_substr($comment, 0, 49);
                $comment = mb_substr($comment, 49);

                cpdf_text($cpdf, $out_text, $text_left, $line_top);
                $line_top -= 14;
            }

            cpdf_set_font($cpdf, $footer_font_name, 13, $ansi_encoding);

            $line_top = 80;

            cpdf_text($cpdf, 'Performed By:_______________ Reviewed By:_______________ Authorized By:_______________', $report_margin, $line_top);

            $line_top -= 15;
            cpdf_text($cpdf, 'Date:_______________', $report_margin+47, $line_top);
            cpdf_text($cpdf, 'Date:_______________', $report_margin+219, $line_top);
            cpdf_text($cpdf, 'Date:_______________', $report_margin+398, $line_top);

            cpdf_end_text($cpdf);
        }

        $page_no++;
    }

    $page_total = $page_no-1;
    $page_no = 1;

    while ($page_no <= $page_total)
    {
        cpdf_set_current_page($cpdf, $page_no);

        DrawPageFooter();

        cpdf_finalize_page($cpdf, $page_no);

        $page_no++;
    }

    cpdf_finalize($cpdf);

    $pdf_filename = ROOT_REAL_PATH."/temp/".$sample_id.".pdf";

    cpdf_save_to_file($cpdf, $pdf_filename);

    cpdf_close($cpdf);

/*
    $len = filesize($pdf_filename);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=hello.pdf");

    readfile($pdf_filename);
*/

//    echo "<script> location.href='/temp/".$sample_id.".pdf'; </script>";

////////// PDF End //////////

?>
<html>
<frameset border="0" framespacing="0" frameborder="0" cols="*">
  <frame src="/temp/<?php echo $sample_id.'.pdf'; ?>" frameborder="0" scrolling="no" noresize="noresize" marginwidth="0" marginheight="0" />
</frameset>
</html>
